package com.solweaver.greetings.dao;

import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.solweaver.greetings.entity.Event;
import com.solweaver.greetings.entity.User;
import com.solweaver.greetings.entity.UserEvent;
import com.solweaver.greetings.entity.UserEventType;

@Repository
public class UserEventDAO extends BaseDAO<UserEvent, Long>{

	public List<UserEvent> findByUser(User user, List<UserEventType> userEventTypeList) {
		List<UserEvent> userEventList = null;
		
		Criterion userEventCriterion = null;
		
		userEventCriterion = Restrictions.eq("user", user);
		
		if(!userEventTypeList.contains(UserEventType.All)){
			userEventCriterion = Restrictions.and(userEventCriterion, Restrictions.in("userEventType", userEventTypeList));
		}

		userEventList = findByCriteria(userEventCriterion);
	
		return userEventList;
	}

	public List<UserEvent> findByUserEvent(Long userId, Long eventId) {
		List<UserEvent> userEventList = null;
		
		User user = new User();
		user.setId(userId);
		
		Event event = new Event();
		event.setId(eventId);
		
		userEventList = findByCriteria(Restrictions.and(Restrictions.eq("user", user), Restrictions.eq("event", event)));
		
		return userEventList;
	}

	public List<UserEvent> findByEvent(Event event) {
		List<UserEvent> userEventList = null;
		
		userEventList = findByCriteria(Restrictions.eq("event", event));
		
		return userEventList;
	}

}
